package com.kamistoat.ruoyi.ruoyicommoncore.utils.bean;

import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Validator;

/**
 * Bean对象属性验证
 * Validator是注解属性验证器，用于快速验证实例是否满足元类中的各种注解(例如 @NotNull之类的)
 */
public class BeanValidators {
    public static void validateWithException(Validator validator, Object object, Class<?>... groups) throws ConstraintViolationException{
        // groups可以为空。若为空，则验证 object元类上所有的注解
        Set<ConstraintViolation<Object>> constraintViolations = validator.validate(object, groups);
        if(!constraintViolations.isEmpty()){
            throw new ConstraintViolationException(constraintViolations);
        }
    }
}
